package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.content.Context;

import androidx.annotation.NonNull;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.entity.Report;
import com.yjhb.android.sbz.mvp.report.ReportPresenter;
import com.yjhb.android.sbz.mvp.report.ReportView;
import com.yjhb.android.sbz.tool.LoadingDialogTool;
import com.yjhb.android.sbz.tool.Tool;
import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.base.BaseEntity;
import com.jojo.android.zxlib.tool.ZxToast;
import com.jojo.android.zxlib.view.dialog.ILoading;
import com.jojo.android.zxlib.view.nine.NineGridImageView;
import com.jojo.android.zxlib.view.nine.NineGridImageViewAdapter;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2020/12/18 17:12
 */
public class TipOffDialog extends BaseDialog implements ReportView {
    private final String TAG = "TipOffDialog";
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.close_iv)
    ImageView closeIv;
    @BindView(R.id.input_et)
    EditText inputEt;
    @BindView(R.id.addImage_iv)
    ImageView addImageIv;
    @BindView(R.id.nineGridImageView)
    NineGridImageView<LocalMedia> nineGridImageView;
    @BindView(R.id.submit_tv)
    TextView submitTv;

    List<LocalMedia> medias = new ArrayList<>();
    NineGridImageViewAdapter adapter;
    //原告时需要填写的内容
    Integer targetUserId;
    ReportPresenter presenter;
    int reportType;
    int state;
    String businessId;
    //原告时需要填写的内容

    //被告
    Report report;
    //被告
    ILoading loadingDialog;

    public static int REPORT_TYPE_CHECK = 1; //乱审核
    public static int REPORT_TYPE_SUBMIT = 2; //乱提交

    public static int STATE_REPORT = 11; //举报
    public static int STATE_REFUTE = 12; //反驳

    public TipOffDialog(@NonNull Activity context, int reportType, Integer targetUserId, String businessId) {
        super(context);
        this.state = STATE_REPORT;
        this.reportType = reportType;
        this.targetUserId = targetUserId;
        this.businessId = businessId;
        initD();
    }

    public TipOffDialog(@NonNull Activity context, Report report) {
        super(context);
        this.state = STATE_REFUTE;
        this.report = report;
        titleTv.setText("辩诉");
        initD();
    }

    private void initD() {
        presenter = new ReportPresenter(this);
        loadingDialog = LoadingDialogTool.createDialog(activity);
    }

    @Override
    protected void init() {
        nineGridImageView.setAdapter(adapter = new NineGridImageViewAdapter<LocalMedia>() {
            @Override
            protected void onDisplayImage(Context context, ImageView imageView, LocalMedia o) {
                Glide.with(context).load(o.getPath())
                        .apply(new RequestOptions().placeholder(com.jojo.android.zxlib.R.drawable.xui_ic_default_img))
                        .into(imageView);
            }
        });
        nineGridImageView.setMaxSize(9);
//        nineGridImageView.setSingleImgSize(ZxTool.dip2px(30));
        nineGridImageView.setShowStyle(0);
    }

    @OnClick(R.id.addImage_iv)
    void addImage() {
        Tool.getPictureSelector(activity, 9).
                selectionMedia(medias)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @OnClick(R.id.close_iv)
    void close() {
        dismiss();
    }

    @OnClick({R.id.submit_tv})
    void submit() {
        if (medias.size() < 1) {
            ZxToast.showBottomToastLight("请添加证据图片。");
            return;
        }
        String content = Tool.getText(inputEt);
        if (TextUtils.isEmpty(content)) {
            ZxToast.showBottomToastLight("请填写内容。");
            return;
        }
        loadingDialog.show();
        if (state == STATE_REPORT) {
            presenter.report(medias, content, targetUserId, reportType, businessId);
        } else {
            presenter.refute(medias, content, report);
        }
    }

    private void refresh() {
        nineGridImageView.setImagesData(new ArrayList<>(medias));
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_tip_off;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMsg(EventMsg msg) {
        if (msg.msg == EventMsg.IMAGE_SELECTED_SUCCESS) {
            medias.clear();
            if (msg != null)
                medias.addAll((List<LocalMedia>) msg.object);
            refresh();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    @Override
    public void onFailure(int code, String msg) {
        loadingDialog.cancel();
        if (code != -1) {
            ZxToast.showINFOBottomToast("失败：" + msg);
        }
    }

    @Override
    public void onUploadIng(int total, int progress) {
        loadingDialog.setTipsText("证据上传中 " + progress + "/" + total);
    }

    @Override
    public void onUploadSuccess() {
        loadingDialog.setTipsText("上传完成,正在提交");
    }

    @Override
    public void onSubmitSuccess() {
        loadingDialog.cancel();
        ZxToast.showSuccessBottomToast("提交成功！");
        dismiss();
    }

    public static class DataBean extends BaseEntity {
        List<String> urls;
        String content;

        public List<String> getUrls() {
            return urls;
        }

        public void setUrls(List<String> urls) {
            this.urls = urls;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

}
